/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#ifndef __dylab_GuardedData_h__
#define __dylab_GuardedData_h__

//===========================================================================================
#include <dylab/utils/thread/Thread.h>

//===========================================================================================
namespace dylab {

    /** 
     * 
     */    
    template < 
        typename T  
    >
    class GuardedData 
    {
    private:
        typedef GuardedData<T> 
			GuardedDataT;

    private:

        /// mutex for controlling concurrent data access 
        Thread::Mutex mMutexGuard;

        /// the exclusive & shared for data access
		Thread::Mutex::ExclusiveLock mExLock;
		mutable Thread::Mutex::SharedLock mShLock;

        /// the guarded data itself
        T mGuardedData; 

    protected:
		//-----------------------------------------------------------------------------
		class DYLAB_EXPORT _ScopedShLock
		{
		private:
			const GuardedDataT * mDataGuard;
		public:
			_ScopedShLock(const GuardedDataT * _dataGuard)
				: mDataGuard(_dataGuard)
					{ DYLAB_ASSERT(mDataGuard != NULL); mDataGuard->_shLock(); }
			const T getData() const
					{ return mDataGuard->mGuardedData; }
			~_ScopedShLock()
					{ mDataGuard->_shUnlock(); }
		};
		friend class _ScopedShLock;
		//-----------------------------------------------------------------------------
		class DYLAB_EXPORT _ScopedExLock
		{
		private:
			GuardedDataT * mDataGuard;
		public:
			_ScopedExLock(GuardedDataT * _dataGuard)
				: mDataGuard(_dataGuard)
					{ DYLAB_ASSERT(mDataGuard != NULL); mDataGuard->_exLock(); }
			T getData() const
					{ return mDataGuard->mGuardedData; }
			void setData(const T & _data) const
					{ mDataGuard->mGuardedData = _data; }
			~_ScopedExLock()
					{ mDataGuard->_exUnlock(); }
		};
		friend class _ScopedExLock;
		//-----------------------------------------------------------------------------

    public:

        /**
         *
         */
        GuardedData()
            : mExLock(&mMutexGuard), mShLock(&mMutexGuard)
                { }

        /**
         *
         */
        explicit GuardedData(T value)
			: mGuardedData(value), mExLock(&mMutexGuard), mShLock(&mMutexGuard)            
                { }

        /**
         *
         */
        virtual ~GuardedData()
                { }

    protected:

        /**
         *
         */
        T _exLock()
				{ mExLock.lock(); return mGuardedData; }

        /**
         *
         */
        const T _shLock() const
                { mShLock.lock(); return mGuardedData; }

        /**
         *
         */
        void _exUnlock()
				{ mExLock.unlock(); }

        /**
         *
         */
        void _shUnlock() const
				{ mShLock.unlock(); }
	};
}
//===========================================================================================
#endif // __dylab_GuardedData_h__
